package com.chazz.dao.impl;/**
 *
 */

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.chazz.dao.CategoryidDao;
import com.chazz.entity.Category;
import com.chazz.tools.ConnectionPools;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @title: CategoridDaoImpl 产品分类的DAO实现层
 * @Author ZhaoHongRui
 * @Date: 2020/9/21 15:51
 * @Version 1.0
 */
public class CategoryidDaoImpl implements CategoryidDao {
    private static ConnectionPools connectionPools = ConnectionPools.getInstace();
    private static DataSource dataSource = connectionPools.getDataSource();
    private static Connection connection = null;

    static {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Category> showAllCategories() {
        List<Category> categories = new ArrayList<>();
        try {
            List<Entity> lists = Db.use().query("select * from category where categoryid = parentid");
            for (Entity entity:lists){
                Category category = new Category();
                category.setCategoryid((Integer) entity.get("categoryid"));
                category.setParentid((Integer) entity.get("parentid"));
                category.setCategoryname((String) entity.get("categoryname"));
                category.setCategoryimg((String) entity.get("Categoryimg"));
                category.setStatus((Integer) entity.get("status"));
                category.setCreatetime(new Date());
                categories.add(category);
            }
            System.out.println(categories.size());
            System.out.println(categories);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return categories;
    }

    /**
     * 根据商品名称进行模糊查询，并返回分类对象
     *
     * @param categoryName
     * @return List<Category>
     * @author Woo
     */
    @Override
    public List<Category> findByCategoryName(String categoryName) {
        List<Entity> entityList = null;
        List<Category> categoryList = new ArrayList<>();
        try {
            entityList = SqlExecutor.query(connection, "select * from category WHERE categoryname REGEXP ? ", new EntityListHandler(), categoryName);

            for (Entity entity : entityList) {
                Category category = new Category();
                category.setCategoryid((Integer) entity.get("categoryid"));
                category.setParentid((Integer) entity.get("parentid"));
                category.setCategoryname((String) entity.get("categoryname"));
                category.setCategoryimg((String) entity.get("categoryimg"));
                category.setStatus((Integer) entity.get("status"));
                category.setCreatetime((Date) entity.get("createTime"));
                category.setUpdatetime((Date) entity.get("updatetime"));
                categoryList.add(category);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        SqlExecutor.query(conn, "select * from " + TABLE_NAME + " where param1 = ?", new EntityListHandler(), "值");

        return categoryList;
    }

    /**
     * 根据商品分类名称查询商品编号
     *
     * @param categoryname
     * @return
     */
    @Override
    public int findCategoryId(String categoryname) {
        int id = 0;
//        id=SqlExecutor.query(connection,"SELECT id FROM category WHERE categoryname=?",new Category(),categoryname);
        try {
            id = Db.use().findAll(Entity.create("category").set("categoryname", categoryname), Category.class).get(0).getCategoryid();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return id;
    }

    /**
     * description: showSmallCategories 返回商品分类的父ID和子ID放入List的集合<br> 
     * version: 1.0 <br>
     * date: 2020/9/24 14:46 <br>
     * author: HongRui <br>
     * @param 
     * @return java.util.List<java.util.List<com.chazz.entity.Category>>
     */
    @Override
    public List<List<Category>> showSmallCategories() {

        List<List<Category>> listLists = new ArrayList<> ();
        try {
            List<Entity> lists = Db.use().query("select * from category where parentid = categoryid");
            for (Entity entity:lists){
                List<Entity> list = Db.use().query("select * from category where parentid = ? and status = 1", entity.get("categoryid"));
                List<Category> categories = new ArrayList<>();
                for (Entity entity1 : list) {
                    Category category = new Category();
                    category.setCategoryid((Integer) entity1.get("categoryid"));
                    category.setParentid((Integer) entity1.get("parentid"));
                    category.setCategoryname((String) entity1.get("categoryname"));
                    category.setCategoryimg((String) entity1.get("Categoryimg"));
                    category.setStatus((Integer) entity1.get("status"));
                    category.setCreatetime(new Date());
                    System.out.println(category.toString());
                    categories.add(category);
                }
                listLists.add(categories);
                System.out.println(listLists.toString());
            }
            System.out.println(listLists.size());
            System.out.println(listLists);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return listLists;
    }

    static class test{
        public static void main(String[] args) {
            List<List<Category>> list = new CategoryidDaoImpl().showSmallCategories();
            System.out.println(list.toString());
        }
    }
}
